// ------------------------------------------------------------------
// Global AnimFrame timer
// ------------------------------------------------------------------

window.requestAnimFrame = (function(){
    return window.requestAnimationFrame  ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame  ||
    window.msRequestAnimationFrame  ||
    function(/* function */ callback, /* DOMElement */ element){
        window.setTimeout(callback, 1000 / 60);
    };
})();

// ------------------------------------------------------------------
// Load the game when html body is loaded
// ------------------------------------------------------------------
function load(){

	initGlobal();
	update();

};

// -------------------------------------------------------------------
// Globals
// -------------------------------------------------------------------
function initGlobal(){

	// Game
    canvas = document.getElementById("game");
    ctx = canvas.getContext("2d");

    // Right Debug
    text = document.getElementById("text");
    txt = text.getContext("2d");
    level_1 = [];
    level_2 = [];
    level_3 = [];
    player = new playerObj();
    msg = new message();
    total_npc =1;
    mouse_x = 0;
    mouse_y= 0;
    mouse_down = false;
    // var temp = 0;
    // for(var i=0;i<10;i++)
    //     for(var j=0; j<10;j++)
    // {
    //     pos_i = i*100;
    //     pos_j = j*100;
    //     eval("aiplayer" + temp + " = new npc(" + pos_i + "," + pos_j + ");");
    //     temp++;
    // }

    canvas.addEventListener('mousemove', ev_mousemove, false);
    canvas.addEventListener('mousedown', ev_mousedown, false);
    canvas.addEventListener('mouseup', ev_mouseup, false);
    aiplayer0 = new npc(100,100);
    aiplayer = new npc(50,100);
    keyStroke = -1;

};

// -------------------------------------------------------------------
// Get the mouse position relative to the canvas element.
// -------------------------------------------------------------------
function ev_mousemove(ev){ 
    if (ev.layerX || ev.layerX == 0) { 
        mouse_x = ev.layerX;
        mouse_y = ev.layerY;
    } 
    else if (ev.offsetX || ev.offsetX == 0) { // For Opera
        mouse_x = ev.offsetX;
        mouse_y = ev.offsetY;
    }
}   

function ev_mousedown(ev)
{
    mouse_down = true;
}

function ev_mouseup(ev)
{
    mouse_down = false;
}

// -------------------------------------------------------------------
// Main update thread 
// -------------------------------------------------------------------
function update(){

   // Listen for key set
   document.onkeydown = checkKey;
   function checkKey(e) {
    e = e || window.event;
    keyStroke = e.keyCode;
    }

controller(); 

animate();
};

// -------------------------------------------------------------------
// Rendering class
// -------------------------------------------------------------------
function animate(){
    // Clear contexts
    ctx.clearRect(0,0,canvas.width,canvas.height);
    txt.clearRect(0,0,text.width,text.height);
    // Keep running update
    drawgrid();
    drawlevel(player);

    player.draw();
    aiplayer.draw();

    for(var i =0; i<level_1.length; i++)
     level_1[i].draw();

 msg.display_list();
 msg.displaytext( aiplayer.id, "War is coming, stay safe"); 

 requestAnimFrame(update);
}